﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace Signage01
{
    class TimelineComponent
    {
        int[] indexOnZOrder;
        int indexChannels = 0;
        List<Rectangle> rectArr = new List<Rectangle>(); 
        private Canvas parentCanvas;
        private List<ResourceTimer> resourceTimerList = new List<ResourceTimer>();
        private List<ResourceGraphicsTimer> resourceGraphicsTimerList = new List<ResourceGraphicsTimer>();


        public Canvas ParentCanvas
        {
            get { return parentCanvas; }
            set { parentCanvas = value; }
        }
        private Timeline selectedTimeline;

        public Timeline SelectedTimeline
        {
            get { return selectedTimeline; }
            set { selectedTimeline = value; }
        }

        private MainWindow mainWindow;

        public TimelineComponent(MainWindow mainWindow, Timeline timeline)
        {
            this.mainWindow = mainWindow;
            this.parentCanvas = mainWindow.campaignCanvas;
            this.selectedTimeline = timeline;
            indexOnZOrder = new int[this.selectedTimeline.ListChannel.Count];
            //rectArr = new Rectangle[this.selectedTimeline.ListChannel.Count];
            initializeRectArr();
            //sort();
            addRectangleToCanvas();
            createResourceTimers();
        }

        private void initializeRectArr()
        {
            for (int i = 0; i < this.selectedTimeline.ListChannel.Count; i++)
            {
                Rectangle rectangle = new Rectangle();
               // rectangle.StrokeThickness = 2;
               // rectangle.Stroke = Brushes.Black;
                
                rectArr.Add(rectangle);
            }
        }
        private void createResourceTimers()
        {
            foreach (Channel ch in this.selectedTimeline.ListChannel)
            {   
                foreach (Resource re in ch.ListResource)
                {
                    ResourceTimer resourceTimer = new ResourceTimer(this.parentCanvas, re, rectArr[indexChannels],this);
                    resourceTimerList.Add(resourceTimer);
                }
                indexChannels++;
            }

            indexChannels = 0;

            foreach (Channel ch in this.selectedTimeline.ListChannel)
            {
                foreach (ResourceGraphics resourceGraphics in ch.ListResourceGraphics)
                {
                    ResourceGraphicsTimer resourceGraphicsTimer = new ResourceGraphicsTimer(mainWindow, resourceGraphics, rectArr[indexChannels], this);
                    resourceGraphicsTimerList.Add(resourceGraphicsTimer);
                }
                indexChannels++;
            }
        }

        public void addRectangleToCanvas()
        {
            for (int index = 0; index < this.selectedTimeline.ListChannel.Count;index++ )
            {
                ScreenDivision screenDivision = this.selectedTimeline.ScTemplate.ListScreenDivision[index];

                double ratioX = (double)this.parentCanvas.ActualWidth / this.selectedTimeline.ScTemplate.ScreenObject.Width;
                double ratioY = (double)this.parentCanvas.ActualHeight / this.selectedTimeline.ScTemplate.ScreenObject.Height;

                double relativeX = screenDivision.RectangleDiv.X * ratioX;
                double relativeY = screenDivision.RectangleDiv.Y * ratioY;

                double relativeWidth = screenDivision.RectangleDiv.Width * ratioX;
                double relativeHeight = screenDivision.RectangleDiv.Height * ratioY;

                Canvas.SetLeft(rectArr[index], relativeX);
                Canvas.SetTop(rectArr[index], relativeY);
                rectArr[index].Width = relativeWidth;
                rectArr[index].Height = relativeHeight;
                Canvas.SetZIndex(rectArr[index], screenDivision.RectangleDiv.ZIndex);

                this.parentCanvas.Children.Add(rectArr[index]);
            }
        }

        public void pause()
        {
            foreach (ResourceTimer resourceTimer in resourceTimerList)
            {
                resourceTimer.pause();
            }

            foreach (ResourceGraphicsTimer resourceGraphicsTimer in resourceGraphicsTimerList)
            {
                resourceGraphicsTimer.pause();
            }

        }

        public void resume()
        {
            foreach (ResourceTimer resourceTimer in resourceTimerList)
            {
                resourceTimer.resume();
            }

            foreach (ResourceGraphicsTimer resourceGraphicsTimer in resourceGraphicsTimerList)
            {
                resourceGraphicsTimer.resume();
            }

        }

        public void deleteResourceTimer(ResourceTimer resourceTimer)
        {
            int index = 0;
            foreach (ResourceTimer resourceTimerTemp in resourceTimerList)
            {
                if (resourceTimerTemp == resourceTimer)
                {
                    break;
                }
                index++;
            }
            resourceTimerList.RemoveAt(index);
        }

        public void deleteResourceGraphicsTimer(ResourceGraphicsTimer resourceGraphicsTimer)
        {
            int index = 0;
            foreach (ResourceGraphicsTimer resourceGraphicsTimerTemp in resourceGraphicsTimerList)
            {
                if (resourceGraphicsTimerTemp == resourceGraphicsTimer)
                {
                    break;
                }
                index++;
            }
            resourceGraphicsTimerList.RemoveAt(index);
        }


        public void stop()
        {
            foreach (ResourceTimer resourceTimerTemp in resourceTimerList)
            {
                if(resourceTimerTemp.resourceMediaElement != null)
                {
                    resourceTimerTemp.resourceMediaElement.Stop();
                }
                if (resourceTimerTemp.timerStart != null)
                {
                    resourceTimerTemp.timerStart.Stop();
                }
                if (resourceTimerTemp.timerEnd != null)
                {
                    resourceTimerTemp.timerEnd.Stop();
                }
            }

            foreach (ResourceGraphicsTimer resourceGraphicsTimerTemp in resourceGraphicsTimerList)
            {
                resourceGraphicsTimerTemp.stop();
            }


        }




    }
}
